package inet.app.vod.view;

import inet.app.vod.activity.R;
import android.app.Activity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.inet.app.framework.util.hardware.KeyboardUtil;

public class AccountEditRowView extends BaseLinearLayout {

	public Activity mActivity;
	public ImageButton mEditBtn;
	public TextView mNameField;
	public TextView mContentField;
	public EditText mEditText;
	public Button mSaveBtn;
	public Button mCancelBtn;
	public RelativeLayout mEditLayout;

	int mPositionInParentLayout;
	EditListener mListener;
	ExpandCollapseAnimation mAnim;

	View.OnClickListener mCancelListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			animateView(ExpandCollapseAnimation.COLLAPSE);
			if (null != mListener) {
				mListener.onEditBtnClick(mPositionInParentLayout);
				KeyboardUtil.hideSoftKeyboard(mActivity);
			}
		}
	};

	View.OnClickListener mSaveListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			KeyboardUtil.hideSoftKeyboard(mActivity);
			saveContent();
		}
	};

	private void saveContent() {
		String content = mEditText.getText().toString();
		if (null != mListener && !mListener.validate()) {
			return;
		}
		if (!TextUtils.isEmpty(content)) {
			mContentField.setText(content);
		}
		animateView(ExpandCollapseAnimation.COLLAPSE);
		if (null != mListener) {
			mListener.onEditBtnClick(mPositionInParentLayout);
		}
	}

	View.OnClickListener mEditListener = new View.OnClickListener() {

		@Override
		public void onClick(View arg0) {

			if (null != mListener) {
				mListener.onEditBtnClick(mPositionInParentLayout);
			}

			if (mEditLayout.getVisibility() != View.VISIBLE) {
				animateView(mEditLayout, ExpandCollapseAnimation.EXPAND);
				return;
			} else {
				animateView(mEditLayout, ExpandCollapseAnimation.COLLAPSE);
				return;
			}

		}
	};

	public AccountEditRowView(Activity activity) {
		super(activity);
		this.mActivity = activity;
		initLayout(activity, R.layout.item_listview_info_account);
		mEditBtn = (ImageButton) findViewById(R.id.edit_img);
		mNameField = (TextView) findViewById(R.id.name_field);
		mContentField = (TextView) findViewById(R.id.content_field);
		mEditText = (EditText) findViewById(R.id.field_edt);
		mEditText
				.setOnEditorActionListener(new EditText.OnEditorActionListener() {
					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_DONE) {
							KeyboardUtil.hideSoftKeyboard(mActivity);
							saveContent();
							return true;
						}
						return false;
					}
				});

		mSaveBtn = (Button) findViewById(R.id.save_btn);
		mSaveBtn.setOnClickListener(mSaveListener);

		mCancelBtn = (Button) findViewById(R.id.cancel_btn);
		mCancelBtn.setOnClickListener(mCancelListener);

		mEditLayout = (RelativeLayout) findViewById(R.id.edit_layout);
		mAnim = new ExpandCollapseAnimation(mEditLayout,
				ExpandCollapseAnimation.COLLAPSE);
		mEditLayout.setAnimation(mAnim);
		mEditBtn.setOnClickListener(mEditListener);
	}

	public void setEditListener(EditListener listener) {
		this.mListener = listener;
	}

	public void setPositionInParentLayout(int pos) {
		mPositionInParentLayout = pos;
	}

	public void setName(String name) {
		if (null != mNameField) {
			mNameField.setText(name);
		}
	}

	public void setContent(String content) {
		if (null != mNameField) {
			mContentField.setText(content);
		}
	}

	public String getContent() {
		if (null != mNameField) {
			return mContentField.getText().toString();
		}
		return "";
	}

	public void setEnableEdit(boolean isEdit) {
		if (null != mEditBtn) {
			mEditBtn.setVisibility(isEdit ? View.VISIBLE : View.INVISIBLE);
		}
	}

	private void animateView(final View target, final int type) {
		mAnim = new ExpandCollapseAnimation(target, type);
		mAnim.setDuration(500);
		target.startAnimation(mAnim);
		if (mEditBtn.getVisibility() == View.VISIBLE) {
			mEditBtn.setVisibility(View.INVISIBLE);
		} else {
			mEditBtn.setVisibility(View.VISIBLE);
		}
	}

	public void animateView(final int type) {
		animateView(mEditLayout, type);
	}

	public int getEditLayoutVisiblily() {
		return mEditLayout.getVisibility();
	}

	public interface EditListener {
		/**
		 * Valide text of edit text before request to server
		 * 
		 * @return true if text is validate
		 */
		public boolean validate();

		public void onEditBtnClick(int position);
	}

}
